<?php
namespace app\index\controller;

use app\common\model\ArticleModel;
use app\common\model\CollegeModel;
use app\common\model\SlideModel;
use app\common\model\TeacherModel;
use app\common\model\TeamMemberModel;
use app\common\model\TeamModel;
use think\Db;

class PeopleController extends BaseController
{
    public function index()
    {

        $keyword = $this->request->param('keyword');
        $college_id = $this->request->param('college_id');
        $page = $this->request->param('page') ? $this->request->param('page') : 1;

        $colleges = CollegeModel::where('status',1)->order('sort','desc')->all();
        if ($keyword){
            if($college_id){
                $college = CollegeModel::where("id",$college_id)->find();
                $teachers = Db::query("select * from sk_teacher where college = ". $college_id ." and status = 1 and (real_name like '%".$keyword."%' or tag_text like '%%".$keyword."')");
                $count = Db::query("select count(*) as count from sk_teacher where college = ". $college_id ." and status = 1 and (real_name like '%".$keyword."%' or tag_text like '%%".$keyword."') limit 1");
                $count = $count[0]['count'];
                $this->assign('count', $count );
                $this->assign('page', $page );
                $this->assign('maxPage', ceil($count/10) );

                $this->assign('college', $college );
                $this->assign('colleges', $colleges);
                $this->assign('teachers', $teachers );
                $this->assign('college_id',$college_id );
                if (is_mobile()){
                    $this->assign('teams',[]);
                    $this->assign('seList',[]);
                    return $this->fetch('/mobile/people/college_people');
                }
                return $this->fetch('college_people');
            }else{
                $teachers = TeacherModel::where('real_name','like', '%'.$keyword.'%')->whereOr('tag_text','like','%'.$keyword.'%')->all();
                $count = TeacherModel::where('real_name','like', '%'.$keyword.'%')->whereOr('tag_text','like','%'.$keyword.'%')->count();
                $this->assign('count', $count );
                $this->assign('page', $page );
                $this->assign('maxPage', ceil($count/10) );

                $this->assign('teachers',$teachers);
                $this->assign('keyword',$keyword);
                $this->assign('college_id',$college_id );
            }

        }else if($college_id){
            $college = CollegeModel::where("id",$college_id)->find();
            $teachers = TeacherModel::where('college',$college_id)->where('status',1)->all();
            $count = TeacherModel::where('college',$college_id)->where('status',1)->count();
            $this->assign('college', $college );
            $this->assign('count', $count );
            $this->assign('page', $page );
            $this->assign('maxPage', ceil($count/10) );
            $this->assign('colleges', $colleges);
            $this->assign('teachers', $teachers );
            $this->assign('college_id',$college_id );
            if (is_mobile()){
                return $this->fetch('/mobile/people/college_people');
            }
            return $this->fetch('college_people');
        }
        else{
            $showCollege = CollegeModel::where('show_people',1)->order('sort','desc')->all();
            foreach ($showCollege as $key => $value){
                $teachers = TeacherModel::where('college',$value['id'])->where('show_home',1)->limit(0,5)->order('sort','desc')->all();
                $showCollege[$key]['teachers']=$teachers;
            }
            $team = TeamModel::limit(0,5)->all();

            $seList = TeacherModel::where("college",21)->limit(0,5)->order('sort','desc')->all();
            $this->assign('seList',$seList );

            $this->assign('showColleges',$showCollege);
            $this->assign('teams',$team);
        }
        $college = null;
        $this->assign('college_id',$college_id );
        $this->assign(['colleges'=> $colleges]);//,'slide'=>$slide
        $this->assign('keyword',$keyword);
        if (is_mobile()){
            $college_id = $college_id ? $college_id : 1;
            $college = CollegeModel::where("id",$college_id)->find();
            $teachers = TeacherModel::where('college',$college_id)->where('status',1)->all();
            $this->assign('college', $college );
            $this->assign('colleges', $colleges);
            $this->assign('teachers', $teachers );
            $this->assign('college_id',$college_id );

            return $this->fetch('/mobile/people/index');
        }
        return $this->fetch();
    }

    public function team(){
        $college_id = $this->request->param('college_id');
        $college = null;
        if($college_id){
            $college = CollegeModel::where("id",$college_id)->find();
        }

        $colleges = CollegeModel::where('status',1)->order('sort','desc')->all();
        $this->assign(['college'=> $college, 'colleges'=> $colleges,'college_id'=>$college_id]);//,'slide'=>$slide
        if ($college_id){
            $team = TeamModel::where('college',$college_id)->all();
        }else{
            $team = TeamModel::all();
        }
        $this->assign('teams',$team);
        if (is_mobile()){
            return $this->fetch('/mobile/people/team');
        }
        return $this->fetch();
    }

    public function teamDetail(){
        $id = $this->request->param('id/d');
        $team = TeamModel::where('id',$id)->find();
        if ($team) {
            $members = TeamMemberModel::where('team_id', $team['id'])->order('id', 'asc')->all();
            $this->assign('team', $team);
            $this->assign('team_members', $members);
            $user = json_decode(session('userInfo'),true);
            $collection = null;
            if ($user){
                $issuer_type = isset($user['teacher_no']) ? 0 : 1;
                $collection = Db::name('collection')->where('type',4)->where('cid',$id)->where('uid',$user['id'])->where('user_type',$issuer_type)->find();
            }
            $this->assign('collection',$collection);
        }else{
            return $this->redirect('people/index');
        }
        if (is_mobile()){
            return $this->fetch('/mobile/people/team_detail');
        }
        return $this->fetch();
    }

    public function detail(){

        return $this->fetch('talents_detail');
    }

    public function talentsdetail(){

        $id = $this->request->param("id");

        if ($id != null){

            $user = json_decode(session('userInfo'),true);
            $collection = null;
            if ($user){
                $issuer_type = isset($user['teacher_no']) ? 0 : 1;
                $collection = Db::name('collection')->where('type',0)->where('cid',$id)->where('uid',$user['id'])->where('user_type',$issuer_type)->find();
            }
            $teacher = TeacherModel::with(['college_detail','specialty_detail'])->where('id',$id)->find();
            $tags = explode(',',$teacher['tag_text']);
            $this->assign(['teacher'=>$teacher, 'tags'=> $tags]);
            $this->assign('collection',$collection);

        }else{
            $this->redirect('index');
        }

        if (is_mobile()){
            return $this->fetch('/mobile/people/talents_detail');
        }

        return $this->fetch('talents_detail');
    }


}
